package cn.iocoder.yudao.module.jikong.controller.admin.jkProductType;

import cn.iocoder.yudao.framework.common.OpenStatusEnum;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductRespVO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

import cn.iocoder.yudao.module.jikong.controller.admin.jkProductType.vo.*;
import cn.iocoder.yudao.module.jikong.dal.dataobject.jkProductType.JkProductTypeDO;
import cn.iocoder.yudao.module.jikong.service.jkProductType.JkProductTypeService;

@Tag(name = "管理后台 - 产品分类")
@RestController
@RequestMapping("/jikong/jk-product-type")
@Validated
public class JkProductTypeController {

    @Resource
    private JkProductTypeService jkProductTypeService;
    @Resource
    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建产品分类")
    @PreAuthorize("@ss.hasPermission('jikong:jk-product-type:create')")
    public CommonResult<Long> createJkProductType(@Valid @RequestBody JkProductTypeSaveReqVO createReqVO) {
        return success(jkProductTypeService.createJkProductType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品分类")
    @PreAuthorize("@ss.hasPermission('jikong:jk-product-type:update')")
    public CommonResult<Boolean> updateJkProductType(@Valid @RequestBody JkProductTypeSaveReqVO updateReqVO) {
        jkProductTypeService.updateJkProductType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jikong:jk-product-type:delete')")
    public CommonResult<Boolean> deleteJkProductType(@RequestParam("id") Long id) {
        jkProductTypeService.deleteJkProductType(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除产品分类")
                @PreAuthorize("@ss.hasPermission('jikong:jk-product-type:delete')")
    public CommonResult<Boolean> deleteJkProductTypeList(@RequestParam("ids") List<Long> ids) {
        jkProductTypeService.deleteJkProductTypeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jikong:jk-product-type:query')")
    public CommonResult<JkProductTypeRespVO> getJkProductType(@RequestParam("id") Long id) {
        JkProductTypeDO jkProductType = jkProductTypeService.getJkProductType(id);

        List<JkProductTypeDO> list = new ArrayList<>();
        list.add(jkProductType);

        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
                convertSet(list, DO -> Long.parseLong(DO.getCreator())));

        JkProductTypeRespVO bean = BeanUtils.toBean(jkProductType, JkProductTypeRespVO.class);
        bean.setCreatorName(userMap.get(Long.parseLong(jkProductType.getCreator())).getNickname());
        return success(bean);
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品分类分页")
    @PreAuthorize("@ss.hasPermission('jikong:jk-product-type:query')")
    public CommonResult<PageResult<JkProductTypeRespVO>> getJkProductTypePage(@Valid JkProductTypePageReqVO pageReqVO) {
        PageResult<JkProductTypeDO> pageResult = jkProductTypeService.getJkProductTypePage(pageReqVO);
        PageResult<JkProductTypeRespVO> bean = BeanUtils.toBean(pageResult, JkProductTypeRespVO.class);

        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
                convertSet(pageResult.getList(), DO -> Long.parseLong(DO.getCreator())));

        bean.getList().forEach(item -> {
            item.setCreatorName(userMap.get(Long.parseLong(item.getCreator())).getNickname());
        });

        return success(bean);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品分类 Excel")
    @PreAuthorize("@ss.hasPermission('jikong:jk-product-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJkProductTypeExcel(@Valid JkProductTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JkProductTypeDO> list = jkProductTypeService.getJkProductTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品分类.xls", "数据", JkProductTypeRespVO.class,
                        BeanUtils.toBean(list, JkProductTypeRespVO.class));
    }


    @GetMapping("/simple-list")
    @Operation(summary = "获得产品分类精简列表", description = "只包含被开启的产品，主要用于前端的下拉选项")
    public CommonResult<List<JkProductTypeDO>> getProductSimpleList() {
        JkProductTypePageReqVO pageReqVO = new JkProductTypePageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        pageReqVO.setStatus(OpenStatusEnum.ENABLE.getStatus());
        List<JkProductTypeDO> list = jkProductTypeService.getJkProductTypePage(pageReqVO).getList();
        return success(list);
    }


}
